package com.zhfi.dataapply.schedule.writer.ipofinance;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhfi.dataapply.entity.data.po.ipofinance.HKProfitPO;
import com.zhfi.dataapply.mapper.ipofinance.HKProfitMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Slf4j
public class HKProfitWriter implements ItemWriter<HKProfitPO> {

    @Autowired
    private HKProfitMapper mapper;

    @Override
    public void write(List<? extends HKProfitPO> list) throws Exception {
        for (HKProfitPO po : list) {
            try {
                UpdateWrapper<HKProfitPO> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("company_id", po.getCompanyId());
                updateWrapper.eq("report_title", po.getReportTitle());
                if (mapper.update(po, updateWrapper) <= 0) {
                    mapper.insert(po);
                }
            } catch (Exception e) {
                log.error("数据插入失败", e);
            }
        }
    }
}